{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Train XGB on TBI signal data\n",
    "\n",
    "Code to train an XGBoost model on the TBI data to predict hypoxemia in the future (low SAO2).\n",
    "\n",
    "Note that the data is private and we are unable to make it publicly available in this repo."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tbi_downstream_prediction import *\n",
    "PATH = \"/homes/gws/hughchen/phase/tbi_subset/\"\n",
    "DPATH = PATH+\"tbi/processed_data/hypoxemia/\"\n",
    "RESULTPATH = PATH+\"results/\"\n",
    "\n",
    "# Set important variables\n",
    "label_type = \"desat_bool92_5_nodesat\"; eta = 0.02\n",
    "hosp_data = \"tbi\"; data_type = \"raw[top11]\"\n",
    "mod_type = \"xgb_{}_eta{}\".format(label_type,eta)\n",
    "\n",
    "# Set up result directory\n",
    "RESDIR = '{}results/{}/'.format(PATH, mod_type)\n",
    "if not os.path.exists(RESDIR): os.makedirs(RESDIR)\n",
    "\n",
    "# Load tbi data\n",
    "y_tbi = np.load(DPATH+\"tbiy.npy\",mmap_mode=\"r\")\n",
    "X_tbi = np.load(DPATH+\"X_tbi_imp_standard.npy\",mmap_mode=\"r\")\n",
    "\n",
    "feats = [\"ECGRATE\", \"ETCO2\", \"ETSEV\", \"ETSEVO\", \"FIO2\", \"NIBPD\", \"NIBPM\", \n",
    "         \"NIBPS\",\"PEAK\", \"PEEP\", \"PIP\", \"RESPRATE\", \"SAO2\", \"TEMP1\", \"TV\"]\n",
    "weird_feats = [\"ETSEV\", \"PIP\", \"PEEP\", \"TV\"]\n",
    "feat_inds = np.array([feats.index(feat) for feat in feats if feat not in weird_feats])    \n",
    "X_tbi2 = X_tbi[:,feat_inds,:]\n",
    "(X_test, y_test, X_valid, y_valid, X_train, y_train) = split_data(DPATH,X_tbi2,y_tbi)\n",
    "feat_lst2 = [feat for feat in feats if feat not in weird_feats]\n",
    "\n",
    "# Set parameters to train model\n",
    "param = {'max_depth':6, 'eta':eta, 'subsample':0.5, 'gamma':1.0, \n",
    "         'min_child_weight':10, 'base_score':y_train.mean(), \n",
    "         'objective':'binary:logistic', 'eval_metric':[\"logloss\"]}"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
